package wsi.gui.tool;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;

import org.rygh.core.html.Html;
import org.rygh.core.html.element.HtmlParagraph;
import org.rygh.core.html.table.HtmlTable;
import org.rygh.core.html.table.TableData;
import org.rygh.core.html.table.TableTuple;
import org.rygh.core.util.UIo;
import org.rygh.core.util.UMath;
import org.rygh.core.util.counter.CounterTuple;
import org.rygh.core.util.counter.SimpleCounter;
import org.rygh.semantic.jena.SDatabase;
import org.rygh.semantic.model.ex.Degreer;
import org.rygh.semantic.model.rdf.Entity;
import org.rygh.semantic.model.rdf.Fact;
import org.rygh.semantic.model.rdf.Predicate;
import org.rygh.semantic.model.rdf.Prefix;
import org.rygh.semantic.sparql.ResultList;
import org.rygh.semantic.sparql.ResultTuple;
import org.rygh.semantic.sparql.SparQL;

import wsi.script.AppConfig;
import wsi.script.QueryTuple;

public class TempTask {

	static String _degree_peek_input_query() {
		TableData data1 = new TableData();
		TableData data = new TableData();
		TableTuple tuple = new TableTuple();

		tuple.add("Query", "Average", "Samples");
		data.add(tuple);

		tuple = new TableTuple();
		tuple.add("Query", "Average");
		data1.add(tuple);

		Degreer deg = new Degreer();
		List<Double> doubles = new ArrayList<>();
		SimpleCounter<Entity> counter = new SimpleCounter<>();
		double begin = .02, end = .2, avg;

		/*
		 * we take the candidates ranked from 20% ~ 60%, to eliminate tail
		 * effect and the very high geeks.
		 */

		for (Entry<String, QueryTuple> e : AppConfig.shared().getQueryMap()
				.entrySet()) {
			counter.clear();
			doubles.clear();

			tuple = new TableTuple();
			tuple.add(e.getKey());

			SparQL query = e.getValue().getQuery();
			query.setLimit(20000);

			ResultList res = SDatabase.shared().select(query);

			for (ResultTuple m : res.getRecords()) {
				Entity entity = m.getX();
				int d = deg.getDegree(entity);

				counter.set(entity, d);
			}

			List<CounterTuple<Entity>> list = counter
					.getListInRange(begin, end);

			for (CounterTuple<Entity> i : list)
				doubles.add((double) i.getCount());

			Collections.sort(doubles);

			avg = UMath.getAverage(doubles, .0);
			tuple.add(avg);
			tuple.add(res.size() + ", " + doubles.size() + ", "
					+ doubles.toString());

			data.add(tuple);

			//

			tuple = new TableTuple();
			tuple.add(e.getKey());
			tuple.add(avg);
			data1.add(tuple);
		}

		Html html = new Html("Degree Distribution");
		html.addElement(new HtmlParagraph(String.format("Range (%.2f, %.2f)",
				begin, end)));
		html.addElement(new HtmlTable("Degree Distribution", data1, false));
		html.addElement(new HtmlTable("Degree Distribution (Detail)", data,
				false));

		UIo.saveFile(new File("e:/a.html"), html.toString());

		return html.toString();
	}

	static String _mbz_1012_sample_facts() {
		SparQL query = new SparQL();
		query.addFact(new Fact("?s", "?p", "?o"));
		query.distinct("?p");

		ResultList res = SDatabase.shared().select(query);

		TableData data = new TableData();
		TableTuple tuple = new TableTuple();

		tuple.add("Subject", "Predicate", "Object");
		data.add(tuple);

		query.setLimit(9);

		Entity subj, obj;

		for (ResultTuple i : res.getRecords()) {
			Predicate p = new Predicate(i.get("?p").getValue());

			query.clear();
			query.addFact(new Fact(new Entity("?s"), p, new Entity("?o")));

			ResultList results = SDatabase.shared().select(query);

			for (ResultTuple j : results.getRecords()) {

				subj = j.get("?s");
				obj = j.get("?o");

				tuple = new TableTuple();
				tuple.add(subj, p, obj);
				data.add(tuple);
			}

		}

		Html html = new Html("Samples");

		for (Entry<String, String> e : Prefix.DEFAULT_PREFIX.getMap()
				.entrySet()) {
			html.addElement(new HtmlParagraph(String.format("%s: %s",
					e.getKey(), e.getValue())));
		}

		html.addElement(new HtmlTable("MusicBrainz", data, false));

		UIo.saveFile(new File("e:/a.html"), html.toString());

		return html.toString();
	}

	public static String run() {
		return null;
	}
}
